package com.iemes.controller.quality;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ClientFormMap;
import com.iemes.entity.MotorcadeInfoFormMap;
import com.iemes.entity.ReturnProductsFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.quality.ReturnProductsMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/quality")
@Controller
public class ReturnProductsController extends BaseController {

	
	@Inject
	private ReturnProductsMapper returnProductsMapper;
	
	
	@RequestMapping("returnproducts")
	private String fine(Model model){
		model.addAttribute("res", findByRes());
		
		//获取所有客户和车牌号
		List<ClientFormMap> clientFormMaps=baseMapper.findByWhere(new ClientFormMap());
		List<MotorcadeInfoFormMap> motorcadeInfoFormMaps=baseMapper.findByWhere(new MotorcadeInfoFormMap());
		model.addAttribute("clientFormMaps", clientFormMaps);
		model.addAttribute("motorcadeInfoFormMaps", motorcadeInfoFormMaps);
		return Common.BACKGROUND_PATH + "/quality/returnproducts/returnproducts_list";
	}
	
	@ResponseBody
	@RequestMapping("returnproducts_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		ReturnProductsFormMap returnProductsFormMap=getFormMap(ReturnProductsFormMap.class);
		List<Map<String,Object>> returnProductsFormMaps=returnProductsMapper.getReturnProductsFormMapByCondition(returnProductsFormMap);
		LuogicPaging(pageNow, pageSize,returnProductsFormMaps);
		return pageView;
	}
	
	@ResponseBody
	@RequestMapping(value="returnproducts_deleteEntity")
	@SystemLog(module="退货处理",methods="退货-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		ReturnProductsFormMap returnProductsFormMap=getFormMap(ReturnProductsFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		for(int i=0;i<str.length;i++){
			returnProductsFormMap.put("state", 1);
			returnProductsFormMap.put("id", str[i]);
			baseMapper.editEntity(returnProductsFormMap);
		}
		return "success";
	}
	
	@RequestMapping("returnproducts_addUI")
	public String addUI(Model model) throws Exception {
		//获取所有客户和车牌号、业务员
		List<ClientFormMap> clientFormMaps=baseMapper.findByWhere(new ClientFormMap());
		List<MotorcadeInfoFormMap> motorcadeInfoFormMaps=baseMapper.findByWhere(new MotorcadeInfoFormMap());
		List<UserFormMap> userFormMaps=returnProductsMapper.getAllUser();
		model.addAttribute("clientFormMaps", clientFormMaps);
		model.addAttribute("motorcadeInfoFormMaps", motorcadeInfoFormMaps);
		model.addAttribute("userFormMaps", userFormMaps);
		return Common.BACKGROUND_PATH + "/quality/returnproducts/returnproducts_add";
	}
	
	@ResponseBody
	@RequestMapping("returnproducts_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="退货处理",methods="退货-新增物料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		ReturnProductsFormMap returnProductsFormMap=getFormMap(ReturnProductsFormMap.class);
		returnProductsFormMap.put("state", 0);
		baseMapper.addEntity(returnProductsFormMap);
		return "success";
	}
	
	@RequestMapping("returnproducts_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		ReturnProductsFormMap returnProductsFormMap=returnProductsMapper.getReturnProductsFormMapById(Integer.parseInt(id));
		model.addAttribute("returnProductsFormMap", returnProductsFormMap);
		//获取所有客户和车牌号、业务员
		List<ClientFormMap> clientFormMaps=baseMapper.findByWhere(new ClientFormMap());
		List<MotorcadeInfoFormMap> motorcadeInfoFormMaps=baseMapper.findByWhere(new MotorcadeInfoFormMap());
		List<UserFormMap> userFormMaps=returnProductsMapper.getAllUser();
		model.addAttribute("clientFormMaps", clientFormMaps);
		model.addAttribute("motorcadeInfoFormMaps", motorcadeInfoFormMaps);
		model.addAttribute("userFormMaps", userFormMaps);
		return Common.BACKGROUND_PATH + "/quality/returnproducts/returnproducts_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="returnproducts_editEntity")
	@SystemLog(module="退货处理",methods="退货-修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		ReturnProductsFormMap returnProductsFormMap=getFormMap(ReturnProductsFormMap.class);
		baseMapper.editEntity(returnProductsFormMap);
		return "success";
	}
	
	
}
